A function to create a contour PDP plot for 2 variables

# A function to return a partial dependence contour plot
pdp_contour <- function(var1, var2, n.trees, model_object){
  # Generate the partial dependence given the variables
  pd <- model_object %>% partial(pred.var = c(var1, var2), n.trees = n.trees)
  # Interpolate the partial dependence values to generate a surface 
  dens <- akima::interp(x = pd[,var1], y = pd[,var2], z = pd$yhat)
  
  # Flattened contour partial dependence plot for 2 variables
  p <- plot_ly(x = dens$x, y = dens$y, z = dens$z, colors = c("blue", "grey", "red"), type = "contour")
  # Add axis labels for 2D plots
  p <- p %>% layout(xaxis = list(title = var1), yaxis = list(title = var2))
  # Return the plot
  return(p)
}

A funciton to create an interactive 3D Partial Dependency Plot

# A function to return a 3D partial dependence plot 
pdp_3D <- function(var1, var2, n.trees, model_object){
  
  pd <- model_object %>% partial(pred.var = c(var1, var2), n.trees = n.trees)
  dens <- akima::interp(x = pd[,var1], y = pd[,var2], z = pd$yhat)
  
  # 3D partial dependence plot with a coloring scale
  p <- plot_ly(x = dens$x, y = dens$y, z = dens$z, colors = c("blue", "grey", "red"), type = "surface")
  
  # Add axis labels for 3D plots
  p <- p %>% layout(scene = list(xaxis = list(title = var1),
                                   yaxis = list(title = var2),
                                   zaxis = list(title = "Partial Dependence")))
  # Return the plot
  return(p)
}

Generate Data

# Choose a random seed to reproduce the results
set.seed(007)
# Generate 5 variables from a multivariate normal distribution
X <- MASS::mvrnorm(n = 100, mu = rep(0, 5), Sigma = diag(5))
# Generate a binary variable 
y <- sample(c(0,1), size = 100, replace = TRUE)
# Put the data into a dataframe
data <- data.frame(cbind(y,X))
# Rename the columns "y", "X1", "X2", "X3", "X4", and "X5"
colnames(data) <- c("y", paste("X",1:5,sep=""))
# Show the first 5 rows of the dataframe
head(data)
##   y         X1          X2          X3          X4         X5
## 1 1 -0.3197056  0.52526155  2.02334405  0.51905837  2.2872472
## 2 0  1.1650659 -0.27572731  0.86249250  0.58753970 -1.1967717
## 3 1 -0.6217105  0.05470977 -0.02490949 -0.07933306 -0.6942925
## 4 1  1.2482132 -0.38824961  0.60063495 -1.17436101 -0.4122930
## 5 0  1.4404015 -0.41702305  1.21648074  0.30872212 -0.9706733
## 6 0  0.7619934 -1.16417517 -1.17653155 -1.60387854 -0.9472799

Construct a GBM model

library(gbm)
gbm1 <- gbm(data = data, 
            formula = y ~., 
            distribution = "bernoulli", 
            n.trees = 200, 
            interaction.depth = 3,
            shrinkage = 0.10, 
            bag.fraction = 0.5)

Single Variable Partial Dependence Plot with pdp

library(pdp)
# PDPs for 1 variable using the pdp package
gbm1 %>% partial(pred.var = "X1", n.trees = 100) %>% plotPartial(smooth = FALSE, lwd = 2)

gbm1 %>% partial(pred.var = "X2", n.trees = 100) %>% plotPartial(smooth = FALSE, lwd = 2)

Interactive 3D Partial Dependence Plot with plotly

# An interactive 3D PDP for two variables using plotly
# Simply click and drag to rotate the surface (in the IDE not the web browser)
library(plotly)
pdp_3D(var1 = "X1", var2 = "X2", n.trees = 100, model_object = gbm1)

An interactive flattened PDP (i.e. contour plot) for two variables (easier to understand sometimes)

# Hover the mouse over the plot to see the plot values (in the IDE not the web browser)
pdp_contour(var1 = "X1", var2 = "X2", n.trees = 100, model_object = gbm1)